package org.oschina.api.discuss.discuss.service;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.oschina.Config;
import org.oschina.Constants;
import org.oschina.Message;
import org.oschina.Module;
import org.oschina.ResultUtil;
import org.oschina.api.BaseService;
import org.oschina.api.comment.CommentCatalog;
import org.oschina.api.comment.service.CommentService;
import org.oschina.api.discuss.discuss.DiscussType;
import org.oschina.api.discuss.discuss.dao.DiscussDAO;
import org.oschina.api.discuss.discuss.model.Discuss;
import org.oschina.api.discuss.discuss.vo.DiscussAccessVo;
import org.oschina.api.discuss.discuss.vo.DiscussIndexDataVo;
import org.oschina.api.discuss.discuss.vo.DiscussVo;
import org.oschina.api.discuss.forum.service.DiscussForumService;
import org.oschina.api.discuss.forum.vo.DiscussForumVo;
import org.oschina.api.discuss.order.service.DiscussOrderService;
import org.oschina.api.discuss.order.vo.DiscussOrderVo;
import org.oschina.api.discuss.tag.service.DiscussTagService;
import org.oschina.api.favorite.favorite.service.FavoriteService;
import org.oschina.api.favorite.type.FavoriteType;
import org.oschina.api.manage.config.service.ConfigService;
import org.oschina.api.manage.module.ModuleType;
import org.oschina.api.manage.module.vo.ModuleVo;
import org.oschina.api.report.ReportType;
import org.oschina.api.report.service.ReportService;
import org.oschina.api.report.vo.ReportVo;
import org.oschina.api.software.service.SoftwareService;
import org.oschina.api.software.vo.SoftwareVo;
import org.oschina.api.user.active.UserActiveType;
import org.oschina.api.user.active.service.UserActiveService;
import org.oschina.api.user.role.service.UserRoleService;
import org.oschina.api.user.user.model.User;
import org.oschina.api.user.user.service.UserService;
import org.oschina.api.user.user.vo.UserVo;
import org.oschina.bean.Pager;
import org.oschina.bean.Result;
import org.oschina.helper.index.IndexResultBean;
import org.oschina.api.index.service.IndexService;
import org.oschina.security.AuthenticationException;
import org.oschina.security.ISecurity;
import org.oschina.security.annotation.Require;
import org.oschina.util.DateUtil;
import org.oschina.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DiscussService extends BaseService {

	@Autowired
	private DiscussDAO discussDAO;
	@Autowired
	private IndexService indexService;
	@Autowired
	private CommentService commentService;
	@Autowired
	private ConfigService configService;
	@Autowired
	private DiscussTagService discussTagService;
	@Autowired
	private DiscussOrderService discussOrderService;
	@Autowired
	private DiscussForumService discussForumService;
	@Autowired
	private SoftwareService softwareService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserRoleService roleService;
	@Autowired
	private ISecurity security;
	@Autowired
	private UserActiveService userActiveService;
	@Autowired
	private FavoriteService favoriteService;
	@Autowired
	private ReportService reportService;
	
	/**
	 * 举报
	 * @param id
	 * @param content
	 * @return
	 */
	@Require(authorization=Module.MODULE_55)
	public Result createReport(int id,String content)
	{
		DiscussVo discussVo=_findVoById(id);
		if(discussVo==null || !_isSimple(discussVo))
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		Result result=reportService.create(id, ReportType.DISCUSS, content);
		
		if(result.isSuccess())
			_setReport(discussVo);
		
		return result;
	}

	/**
	 * 返回举报详细信息
	 * @param id
	 * @return
	 */
	@Require(authorization=Module.MODULE_54)
	public ReportVo findReportConcrete(int id)
	{
		return reportService.findVoByMajorAndType(id, ReportType.DISCUSS);
	}
	
	/**
	 * 返回举报
	 * @param pn
	 * @return
	 */
	@Require(authorization=Module.MODULE_54)
	public Pager<DiscussVo> findReport(int pn)
	{
		Pager<DiscussVo> pager=new Pager(pn,_getCountByType(DiscussType.REPORT),_getPageCount(),null);
		if(pager.getData_sum()>0)
			pager.setData(_findVoByType(DiscussType.REPORT, pager.getStart(), pager.getPage_count()));
			
		return pager;
	}
	
	/**
	 * 关注
	 * @param id
	 * @return
	 */
	@Require
	public Result createFavorite(int id)
	{
		DiscussVo discussVo=_findVoById(id);
		if(discussVo==null || !_isSimple(discussVo))
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		User user=userService.getAuthenticationInfo(getToken());
		Result result=favoriteService.create(user.getId(), discussVo.getTitle(), id, FavoriteType.DISCUSS);
		
		if(result.isSuccess())
			_updateFavoriteCount_plus(discussVo);
		
		return result;
	}
	
	/**
	 * 删除关注
	 * @param id
	 * @return
	 */
	@Require
	public Result deleteFavorite(int id)
	{
		DiscussVo discussVo=_findVoById(id);
		if(discussVo==null || !_isSimple(discussVo))
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		User user=userService.getAuthenticationInfo(getToken());
		Result result=favoriteService.delete(user.getId(), id, FavoriteType.DISCUSS);
		
		if(result.isSuccess())
			_updateFavoriteCount_minus(discussVo);
		
		return result;
	}
	/**
	 * 设置排序
	 * @param id
	 * @param order
	 * @return
	 */
	@Require(authorization=Module.MODULE_36)
	public Result updateOrder(int id,int order)
	{
		DiscussVo discussVo=_findVoById(id);
		if(discussVo==null || !_isSimple(discussVo))
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		DiscussOrderVo discussOrderVo=discussOrderService.findVoById(order);
		if(discussOrderVo==null)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		_updateOrder(id, order);
		return ResultUtil.getSuccess(Message.UPDATE_SUCCESS_DELAY);
	}
	
	/**
	 * 恢复已删除
	 * @param id
	 * @return
	 */
	@Require
	public Result recoverDelete(int id)
	{
		DiscussVo discussVo=_findVoById(id);
		if(discussVo==null || (!_isDelete(discussVo) && !_isReport(discussVo)))
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		if(_isDelete(discussVo) && !security.require(new String[]{Module.MODULE_35}, getToken(), false))
			throw new AuthenticationException();
		
		if(_isReport(discussVo))
		{
			if(!security.require(new String[]{Module.MODULE_54}, getToken(), false))
				throw new AuthenticationException();
			else
				reportService.deleteByMajorAndType(id, ReportType.DISCUSS);
		}
		
		_updateType(discussVo,DiscussType.SIMPLE);
		return ResultUtil.getSuccess(Message.UPDATE_SUCCESS);
	}
	
	/**
	 * 返回已删除
	 * @param pn
	 * @return
	 */
	@Require(authorization=Module.MODULE_35)
	public Pager<DiscussVo> findDelete(int pn)
	{
		Pager<DiscussVo> pager=new Pager(pn,_getCountByType(DiscussType.DELETE),_getPageCount(),null);
		if(pager.getData_sum()>0)
			pager.setData(_findVoByType(DiscussType.DELETE, pager.getStart(), pager.getPage_count()));
			
		return pager;
	}
	
	/**
	 * 移动板块
	 * @param id
	 * @param forum
	 * @return
	 */
	@Require(authorization=Module.MODULE_29)
	public Result moveForum(int id,int forum)
	{
		DiscussVo discussVo=_findVoById(id);
		if(discussVo==null)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		DiscussForumVo discussForumVo=discussForumService._findVoById(forum);
		if(discussForumVo==null)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		_updateForum(discussVo, discussForumVo);
		return ResultUtil.getSuccess(Message.UPDATE_SUCCESS);
	}
	
	/**
	 * 编辑
	 * @param id
	 * @param title
	 * @param content
	 * @return
	 */
	@Require
	public Result edit(int id,String title,String content)
	{
		DiscussVo discussVo=_findVoById(id);
		if(discussVo==null || !_isSimple(discussVo))
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		_require(getToken(),discussVo,new String[]{Module.MODULE_26},new String[]{Module.MODULE_25});
		
		Discuss discuss=new Discuss();
		discuss.setId(id);
		discuss.setTitle(title);
		discuss.setContent(content);
		
		_edit(discuss);
		return ResultUtil.getSuccess(discuss);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Require
	public Result delete(int id)
	{
		DiscussVo discussVo=_findVoById(id);
		if(discussVo==null || (!_isSimple(discussVo) && !_isReport(discussVo)))
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		if(_isSimple(discussVo))
			_require(getToken(),discussVo,new String[]{Module.MODULE_28},new String[]{Module.MODULE_27});
		else if(_isReport(discussVo))
		{
			if(!security.require(new String[]{Module.MODULE_54}, getToken(), false))
				throw new AuthenticationException();
			else
				reportService.deleteByMajorAndType(id, ReportType.DISCUSS);
		}
		
		_setDelete(discussVo);
		return ResultUtil.getSuccess(Message.DELETE_SUCCESS);
	}
	
	/**
	 * 返回审核
	 * @param pn
	 * @return
	 */
	@Require(authorization=Module.MODULE_18)
	public Pager<DiscussVo> findCheck(int pn)
	{
		Pager pager=new Pager(pn,_getCountByType(DiscussType.CHECK),_getPageCount(),null);
		if(pager.getData_sum()>0)
			pager.setData(_findVoByType(DiscussType.CHECK, pager.getStart(), pager.getPage_count()));
		
		return pager;
	}
	
	/**
	 * 审核通过
	 * @param id
	 * @return
	 */
	@Require(authorization=Module.MODULE_18)
	public Result checkPass(int id)
	{
		DiscussVo discussVo=_findVoById(id);
		if(discussVo==null || !_isCheck(discussVo))
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		_setSimple(discussVo);
		return ResultUtil.getSuccess(Message.CHECK_SUCCESS);
	}
	
	/**
	 * 审核不通过
	 * @param id
	 * @return
	 */
	@Require(authorization=Module.MODULE_18)
	public Result checkFailure(int id)
	{
		DiscussVo discussVo=_findVoById(id);
		if(discussVo==null || !_isCheck(discussVo))
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		_setDelete(discussVo);
		return ResultUtil.getSuccess(Message.CHECK_SUCCESS);
	}
	
	/**
	 * 返回首页讨论数据
	 * @return
	 */
	public DiscussIndexDataVo getIndex()
	{
		int planePageCount=(Integer) configService.getValueById(Config.CONFIG_9, Integer.class);
		int verticalPageCount=(Integer) configService.getValueById(Config.CONFIG_10, Integer.class);
		return _getIndexData(planePageCount,verticalPageCount,DiscussType.SIMPLE);
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	public DiscussAccessVo findById(int id)
	{
		DiscussVo discussVo=_findVoById(id);
		if(discussVo==null || !_isSimple(discussVo))
			return null;
		
		discussVo.setForum(discussForumService._findVoById(discussVo.getForum().getId()));
		
		DiscussAccessVo discussAccessVo=new DiscussAccessVo();
		discussAccessVo.setDiscuss(discussVo);
		discussAccessVo.setTags(discussTagService._findVoByDiscuss(id));
		discussAccessVo.setRelated(_findByRelated(discussVo, 0, _getRelatedPageCount()));
		
		User user=userService.getAuthenticationInfo(getToken());
		if(user!=null)
		{
			discussAccessVo.setFavorite(favoriteService._isFavorite(user.getId(), id, FavoriteType.DISCUSS));
			List<ModuleVo> module=roleService._findVoByRoleAndType(user, ModuleType.MODULE_11);
			if(_isAuthor(discussVo, user))
			{
				module.addAll(roleService._findVoByRoleAndType(user, ModuleType.MODULE_10));
				module=ListUtil.removeRepeat(module);
			}
			discussAccessVo.setModule(module);
		}
		
		_updateViewsCount_plus(discussVo);
		
		return discussAccessVo;
	}
	private int _getRelatedPageCount()
	{
		return (Integer) configService.getValueById(Config.CONFIG_32, Integer.class);
	}
	
	/**
	 * 发表评论
	 * @param id
	 * @param content
	 * @param quote
	 * @return
	 */
	@Require
	public Result createComment(int id,String content,int quote)
	{
		DiscussVo discussVo=_findVoById(id);
		if(discussVo==null || !_isSimple(discussVo))
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		Result result=commentService._create(discussVo.getId(),discussVo.getComment_id(), content, quote ,CommentCatalog.DISCUSS);
		
		if(result.isSuccess())
		{
			_updateCommentCount_plus(discussVo);
			_updateLastTimeAndLastAuthor(discussVo,userService.getAuthenticationInfo(getToken()));
			userActiveService.create(id, discussVo.getTitle(), content, UserActiveType.DISCUSS_COMMENT);
		}
		
		return result;
	}
	
	/**
	 * 根据板块返回
	 * @param forum
	 * @param pn
	 * @return
	 */
	public Pager<DiscussVo> findByForum(int forum,int pn)
	{
		Pager pager=new Pager(pn,_getCountByforumAndType(forum,DiscussType.SIMPLE),_getPageCount(),null);
		if(pager.getData_sum()>0)
			pager.setData(_findVoByForumAndType(forum, DiscussType.SIMPLE, pager.getStart(), pager.getPage_count()));
		
		return pager;
	}
	
	/**
	 * 根据软件返回
	 * @param tag
	 * @param pn
	 * @return
	 */
	public Pager<DiscussVo> findVoBySoftware(int id,int pn)
	{
		SoftwareVo software=softwareService._findVoById(id);
		if(software==null)
			return null;
		
		Pager pager=new Pager(pn,_getCountByTagAndType(software.getName(),DiscussType.SIMPLE),_getPageCount(),null);
		if(pager.getData_sum()>0)
			pager.setData(_findVoByTagAndType(software.getName(), DiscussType.SIMPLE, pager.getStart(), pager.getPage_count()));
		
		return pager;
	}
	
	/**
	 * 创建
	 * @param discuss
	 * @param tags
	 * @return
	 */
	@Require(authorization=Module.MODULE_17)
	public Result create(Discuss discuss,String tags[])
	{
		if(discussForumService._findVoById(discuss.getForum())==null)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		discuss=_create(discuss,userService.getVo(getToken()),getIp(),tags);
		userActiveService.create(discuss.getId(), discuss.getTitle(), discuss.getContent(), UserActiveType.DISCUSS);
		return ResultUtil.getSuccess(discuss);
	}
	
	/**
	 * 根据搜索返回
	 * @param keywords
	 * @param first
	 * @param max
	 * @return
	 */
	public Pager<DiscussVo> findVoBySearch(String keyword,int pn)
	{
		String keywords[]=StringUtils.split(keyword," ");
		Pager pager=new Pager(pn,0,_getPageCount(),null);
		IndexResultBean indexResult=_searchVoByType(keywords, DiscussType.SIMPLE, pager.getStart(), pager.getPage_count());
		pager.setData_sum(indexResult.getSum());
		pager.setData(indexResult.getResult());
		return pager;
	}
	
	/**
	 * 删除评论
	 * @param id
	 * @return
	 */
	public void _deleteComment(int id)
	{
		DiscussVo discussVo=_findVoById(id);
		if(discussVo==null || !_isSimple(discussVo))
			return;
		
		_updateCommentCount_minus(discussVo);
	}
	
	/**
	 * 举报评论
	 * @param id
	 */
	public void _reportComment(int id)
	{
		DiscussVo discussVo=_findVoById(id);
		if(discussVo==null || !_isSimple(discussVo))
			return;
		
		_updateCommentCount_minus(discussVo);
	}
	
	/**
	 * 恢复评论
	 * @param id
	 */
	public void _recoverComment(int id)
	{
		DiscussVo discussVo=_findVoById(id);
		if(discussVo==null || !_isSimple(discussVo))
			return;
		
		_updateCommentCount_plus(discussVo);
	}

	/**
	 * 返回首页数据
	 * @param planePageCount
	 * @param verticalPageCount
	 * @param type
	 * @return
	 */
	private DiscussIndexDataVo _getIndexData(int planePageCount,int verticalPageCount,int type)
	{
		return discussDAO.getIndexData(planePageCount, verticalPageCount, type);
	}
	
	/**
	 * 根据类型搜索
	 * @param keywords
	 * @param first
	 * @param max
	 * @return
	 */
	private IndexResultBean<DiscussVo> _searchVoByType(String keywords[],int type,int first,int max)
	{
		return discussDAO.searchVoByType(keywords, type,first, max);
	}
	
	/**
	 * 根据标签和类型返回
	 * @param tag
	 * @param type
	 * @param first
	 * @param max
	 * @return
	 */
	private List<DiscussVo> _findVoByTagAndType(String tag,int type,int first,int max)
	{
		return discussDAO.findVoByTagAndType(tag, type, first, max);
	}
	
	/**
	 * 根据标签和类型返回数量
	 * @param tag
	 * @param type
	 * @return
	 */
	private int _getCountByTagAndType(String tag,int type)
	{
		return discussDAO.getCountByTagAndType(tag, type);
	}
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	private DiscussVo _findVoById(int id)
	{
		return discussDAO.findVoById(id);
	}
	
	/**
	 * 根据类型返回
	 * @param type
	 * @param first
	 * @param max
	 * @return
	 */
	private List<DiscussVo> _findVoByType(int type,int first,int max)
	{
		return discussDAO.findVoByType(type, first, max);
	}
	
	/**
	 * 根据类型返回数量
	 * @param type
	 * @return
	 */
	private int _getCountByType(int type)
	{
		return discussDAO.getCountByType(type);
	}
	
	/**
	 * 根据板块和类型返回
	 * @param forum
	 * @param type
	 * @param first
	 * @param max
	 * @return
	 */
	private List<DiscussVo> _findVoByForumAndType(int forum,int type,int first,int max)
	{
		return discussDAO.findVoByForumAndType(forum, type, first, max);
	}
	
	/**
	 * 根据板块和类型返回数量
	 * @param forum
	 * @param type
	 * @return
	 */
	private int _getCountByforumAndType(int forum,int type)
	{
		return discussDAO.getCountByforumAndType(forum, type);
	}
	
	/**
	 * 返回相关讨论
	 * @param discuss
	 * @param first
	 * @param max
	 * @return
	 */
	private List<DiscussVo> _findByRelated(DiscussVo discuss,int first,int max)
	{
		return discussDAO.findByRelated(discuss, first, max);
	}
	
	/**
	 * 创建
	 * @param discuss
	 * @return
	 */
	private Discuss _create(Discuss discuss,UserVo author,String ip,String tags[])
	{
		discuss.setAuthor(author.getId());
		discuss.setLast_author(author.getId());
		discuss.setAuthor_ip(ip);
		discuss.setLast_author_ip(ip);
		
		discuss.setId(_getIndex());
		if(discussForumService._findVoById(discuss.getForum()).getCheck())
			discuss.setType(DiscussType.CHECK);
		else
			discuss.setType(DiscussType.SIMPLE);
		
		discuss.setOrder(_getDefaultOrder());
		discuss.setComment_id(_getCommentIndex());
		discuss.setComment_count(0);
		discuss.setViews_count(0);
		discuss.setFavorite_count(0);
		discuss.setCreate_time(DateUtil.now());
		discuss.setLast_time(discuss.getCreate_time());
		
		discussDAO.create(discuss);
		discussTagService._create(discuss.getId(),tags);
		return discuss;
	}
	
	/**
	 * 更新最后回复时间和作者
	 * @param discussVo
	 * @param user
	 */
	private void _updateLastTimeAndLastAuthor(DiscussVo discussVo,User user)
	{
		UserVo author=new UserVo();
		author.setId(user.getId());
		author.setUsername(user.getUsername());
		discussDAO.updateLastTimeAndLastAuthor(discussVo.getId(),author,user.getIp(),DateUtil.now());
	}
	
	/**
	 * 更新评论数量
	 * @param id
	 * @param count
	 */
	private void _updateCommentCount_plus(DiscussVo discussVo)
	{
		discussDAO.updateCommentCount(discussVo.getId(), 1);
	}
	
	/**
	 * 更新评论数量
	 * @param id
	 * @param count
	 */
	private void _updateCommentCount_minus(DiscussVo discussVo)
	{
		discussDAO.updateCommentCount(discussVo.getId(), -1);
	}
	
	/**
	 * 更新关注数量
	 * @param id
	 * @param count
	 */
	private void _updateFavoriteCount_plus(DiscussVo discussVo)
	{
		discussDAO.updateFavoriteCount(discussVo.getId(), 1);
	}
	
	/**
	 * 更新关注数量
	 * @param id
	 * @param count
	 */
	private void _updateFavoriteCount_minus(DiscussVo discussVo)
	{
		discussDAO.updateFavoriteCount(discussVo.getId(), -1);
	}
	
	/**
	 * 更新类型
	 * @param discussVo
	 * @param type
	 * @return
	 */
	private int _updateType(DiscussVo discussVo,int type)
	{
		return discussDAO.updateType(discussVo.getId(), type);
	}
	
	/**
	 * 更新板块
	 * @param id
	 * @param forum
	 * @return
	 */
	private int _updateForum(DiscussVo discussVo,DiscussForumVo discussForumVo)
	{
		return discussDAO.updateForum(discussVo.getId(), discussForumVo.getId());
	}
	
	/**
	 * 编辑
	 * @param software
	 * @return
	 */
	private int _edit(Discuss discuss)
	{
		return discussDAO.edit(discuss);
	}
	
	/**
	 * 更新排序
	 * @param id
	 * @param order
	 * @return
	 */
	private int _updateOrder(int id,int order)
	{
		return discussDAO.updateOrder(id, order);
	}
	
	/**
	 * 更新浏览量
	 * @param id
	 * @param count
	 */
	private void _updateViewsCount_plus(DiscussVo discussVo)
	{
		discussDAO.updateViewsCount(discussVo.getId(), 1);
	}
	
	private int _getCommentIndex()
	{
		return commentService._getIndex();
	}
	
	private int _getIndex()
	{
		return indexService.getMajorById(Constants.INDEX_DISCUSS);
	}
	
	private boolean _isSimple(DiscussVo discussVo)
	{
		return discussVo.getType().equals(DiscussType.SIMPLE);
	}
	
	private boolean _isCheck(DiscussVo discussVo)
	{
		return discussVo.getType().equals(DiscussType.CHECK);
	}
	
	private boolean _isDelete(DiscussVo discussVo)
	{
		return discussVo.getType().equals(DiscussType.DELETE);
	}
	
	private boolean _isReport(DiscussVo discussVo)
	{
		return discussVo.getType().equals(DiscussType.REPORT);
	}
	
	private int _getPageCount()
	{
		return (Integer) configService.getValueById(Config.CONFIG_8, Integer.class);
	}
	
	public int _getDefaultOrder()
	{
		return discussOrderService.getDefault().getId();
	}
	
	/**
	 * 是否是作者
	 * @param discussVo
	 * @param user
	 * @return
	 */
	private boolean _isAuthor(DiscussVo discussVo,User user)
	{
		return discussVo.getAuthor().getId().equals(user.getId());
	}
	
	private void _setSimple(DiscussVo discussVo)
	{
		_updateType(discussVo,DiscussType.SIMPLE);
	}
	
	private void _setCheck(DiscussVo discussVo)
	{
		_updateType(discussVo,DiscussType.CHECK);
	}
	
	private void _setDelete(DiscussVo discussVo)
	{
		_updateType(discussVo,DiscussType.DELETE);
	}
	
	private void _setReport(DiscussVo discussVo)
	{
		_updateType(discussVo,DiscussType.REPORT);
	}
	
	/**
	 * 验证
	 * @param token 令牌
	 * @param discussVo 主题
	 * @param authority 主权限
	 * @param authorityOr
	 * @param authorAuthority 作者权限
	 * @param authorAuthorityOr
	 */
	private void _require(String token,DiscussVo discussVo,String authority[],String authorAuthority[])
	{
		_require(token,discussVo,authority,false,authorAuthority,false);
	}
	private void _require(String token,DiscussVo discussVo,String authority[],boolean authorityOr,String authorAuthority[],boolean authorAuthorityOr)
	{
		if(!security.require(authority, token, authorityOr))
		{
			User user=userService.getAuthenticationInfo(token);
			if(!_isAuthor(discussVo,user) || !security.require(authorAuthority, token, authorAuthorityOr))
				throw new AuthenticationException();
		}
	}
}